class RecordPanel extends BasePanel {

    private scroller: eui.Scroller;
    private group: eui.Group;

    private info: RecordDetailsInfoItem;

    private pageTool: PageTool;

    list: any[];
    detailsList: any[];

    private timestamp: number = 0;
    private page: number = 1;

    public constructor() {
        super();
        this.skinName = "RecordPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.info.btn_reback.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_RECORD_LIST, this.updateList, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_RECORD_DETAILS_LIST, this.updateDetails, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_RECORD_DETAILS_TYPE, this.infoTypeHandler, this);

        this.pageTool.addEventListener(CommonEventType.CHANGED, this.onUpdatePage, this);
    }

    private infoTypeHandler(type: number){
        this.info.setType(type);
    }

    private onUpdatePage() {
        if (this.page != this.pageTool.page) {
            this.page = this.pageTool.page;

            let start: number = (this.page - 1) * GameConst.Count_Page_Record;
            let end: number = start + GameConst.Count_Page_Record - 1;
            gameManager.socketManager.send(19, {
                start: start,
                end: end,
                time: this.timestamp
            });
        }
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.info.btn_reback:
                this.updateList(this.list);
                break;
        }
    }

    updateList(data: any): void {
        this.clean();

        this.skinState = 'normal';

        if (data.hasOwnProperty('zong_shu')) {
            this.pageTool.setScope(1, Math.ceil(+data.zong_shu / GameConst.Count_Page_Record));
        }

        let list: any[] = [];

        if (data.hasOwnProperty('return_data')) {
            list = data.return_data;
        }
        else {
            list = data;
        }

        this.list = list;

        let item: RecordItem;
        let some: any;
        for (let i = 0; i < list.length; i++) {
            some = list[i];
            some.rank = i + 1;

            item = new RecordItem();
            this.group.addChild(item);

            item.update(some);
        }
    }

    updateDetails(list: any[]): void {
        this.clean();

        this.skinState = 'details';

        this.detailsList = list;

        let item: RecordDetailsItem;
        let some: any[];
        for (let i = 0; i < list.length; i++) {
            some = list[i];

            item = new RecordDetailsItem();
            this.group.addChild(item);

            item.update(some);
            item.rank = i + 1;
        }
    }

    updateDetailsInfo(rank, roomid, time) {
        this.info.rank = rank;
        this.info.roomid = roomid;
        this.info.time = time;
    }

    clean() {
        this.scroller.stopAnimation();
        this.group.removeChildren();
        this.group.scrollV = 0;
        this.group.validateNow();
    }

    show() {
        super.show();

        console.log('--------------------');

        this.pageTool.setPage(1);
    }

    hide() {
        super.hide();
        game.isReplaying = false;
    }
}